home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Robocode / robocode-setup-1.0.7.jar / extract.jar / robots / sample / Walls.java < prev    next >
Encoding:
Java Source  |  2005-02-18  |  1.7 KB  |  68 lines

  1. package sample;
  2. import robocode.*;
  3. /**
  4.  * Walls - a sample robot by Mathew Nelson
  5.  * 
  6.  * Moves around the outer edge with the gun facing in.
  7.  */
  8. public class Walls extends Robot {
  9.  
  10.     boolean peek;        // Don't turn if there's a robot there
  11.     double moveAmount;    // How much to move
  12.  
  13.     /**
  14.      * run: Move around the walls
  15.      */
  16.     public void run() {
  17.         // Initialize moveAmount to the maximum possible for this battlefield.
  18.         moveAmount = Math.max(getBattleFieldWidth(),getBattleFieldHeight());
  19.         // Initialize peek to false
  20.         peek = false;
  21.         
  22.         // turnLeft to face a wall.
  23.         // getHeading() % 90 means the remainder of 
  24.         // getHeading() divided by 90.
  25.         turnLeft(getHeading() % 90);
  26.         ahead(moveAmount);
  27.         // Turn the gun to turn right 90 degrees.
  28.         peek = true;
  29.         turnGunRight(90);
  30.         turnRight(90);
  31.         
  32.         while (true)
  33.         {
  34.             // Look before we turn when ahead() completes.
  35.             peek = true;
  36.             // Move up the wall
  37.             ahead(moveAmount);
  38.             // Don't look now
  39.             peek = false;
  40.             // Turn to the next wall
  41.             turnRight(90);
  42.         }
  43.     }
  44.     
  45.     /**
  46.      * onHitRobot:  Move away a bit.
  47.      */    
  48.     public void onHitRobot(HitRobotEvent e) {
  49.         // If he's in front of us, set back up a bit.
  50.         if (e.getBearing() > -90 && e.getBearing() < 90)
  51.             back(100);
  52.         // else he's in back of us, so set ahead a bit.
  53.         else
  54.             ahead(100);
  55.     }
  56.  
  57.     /**
  58.      * onScannedRobot:  Fire!
  59.      */    
  60.     public void onScannedRobot(ScannedRobotEvent e) {
  61.         fire(2);
  62.         // Note that scan is called automatically when the robot is moving.
  63.         // By calling it manually here, we make sure we generate another scan event if there's a robot on the next 
  64.         // wall, so that we do not start moving up it until it's gone.
  65.         if (peek)
  66.             scan();
  67.     }
  68. }